package net.minecraft.src;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * PPHConfig will handle config for managing ID's to make the mod more compatible with other mods
 * Useage for the coding:
 * PPHConfig.getConfig().getId("Block/Item Name", 231); //the 231 is the default id for that block
 * @author Johnnei
 *
 */
public class PPHConfig {
	
	/**
	 * Single Instance Manner
	 */
	private static PPHConfig config = new PPHConfig();
	
	/**
	 * HashMap of Strings attached to a Integer, in otherwords: The itemName attached to the ID as in the config
	 */
	private HashMap<String, Integer> configData;
	
	/**
	 * Read the config and check if all files and goodies are created on the running system
	 */
	private PPHConfig() {
		configData = new HashMap<String, Integer>();
		//Create folder for our mod if it doesn't exists
		new File("./pph/").mkdir();
		//Check if the file exists
		File configFile = new File("./pph/ids.cfg");
		if(configFile.exists()) {
			//File Exists lets read it
			try {
				BufferedReader conf = new BufferedReader(new FileReader(configFile));
				String line;
				//Read Until end of file
				while((line = conf.readLine()) != null) {
					String[] data = line.split("=");
					if(data.length < 2) //Missing Data
						continue;
					if(!isInt(data[1])) //Invalid Data
						continue;
					//Valid data, Add it!
					configData.put(data[0], Integer.parseInt(data[1]));
				}
				conf.close(); //Close the file up so we can rewrite it if needed later on
			} catch (FileNotFoundException filenotfound) {
				//Can't happen, so I wont do anything with it.
			} catch (IOException e) {
				System.err.println(e.getMessage());
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Check if the given string is an actual Integer
	 * @param s the string to be checked
	 * @return if the string is an integer true otherwise false
	 */
	private boolean isInt(String s) {
		try {
			Integer.parseInt(s);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * Get the Config to read from
	 * @return PPHConfing instance
	 */
	public static PPHConfig getConfig() {
		return config;
	}
	
	/**
	 * Writes the config to the system
	 */
	private void saveConfig() {
		try {
			//Remove the original file
			File configFile = new File("./pph/ids.cfg");
			configFile.delete();
			//Instance for the output
			BufferedWriter out = new BufferedWriter(new FileWriter(configFile));
			
			//Iterate through all elements of the hashmap and add them to the output
			for(Map.Entry<String, Integer> entry : configData.entrySet()) {
				String itemName = entry.getKey();
				int id = entry.getValue();
				out.write(itemName + "=" + id + "\n");
			}
			
			//Close the file
			out.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Get the id from the config file.
	 * @param itemName Item Name to look for in the config (should be unique)
	 * @param defaultId The default number incase its not (yet) specified
	 */
	public int getId(String itemName, int defaultId) {
		if(configData.get(itemName) != null) {
			//There is a ID registered in the config, use that one
			return configData.get(itemName);
		} else {
			//No ID Registered, Lets register the default one and use that one for the time being, Also save the new config to the system
			configData.put(itemName, defaultId);
			saveConfig();
			return defaultId;
		}
	}

}
